package com.hq.fxh.No3_TuiGuang.Fragment;

import android.os.Handler;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hq.fxh.CustomerView.countview.RiseNumberTextView;
import com.hq.fxh.HttpApi.ArticleServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.ShareArticlesRes;
import com.hq.fxh.Modle.SumStatisticalRes;
import com.hq.fxh.No0_ChengQu.Fragment.BaseFragment;
import com.hq.fxh.No3_TuiGuang.Adaoter.TuiGuangAdapter;
import com.hq.fxh.R;
import com.hq.fxh.Utils.LogUtils;
import com.hq.fxh.Utils.LongNumTransUtils;
import com.hq.fxh.Utils.SharedPreferenceUtils;


import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;

import cn.jiguang.analytics.android.api.JAnalyticsInterface;

/**
 * @author 大圣
 * @ClassName Tg_GeRen_Fragment
 * @Description :企业账号推广页面——个人页面
 * @Created 16/5/18
 * @Email qwqe601660957@163.com
 */
public class Tg_GeRen_Fragment extends BaseFragment {
    /**
     * 曝光
     */
    private LinearLayout llExposeNum;
    private RiseNumberTextView tvAmountExposeNum;
    /**
     * 点击
     */
    private LinearLayout llClickNum;
    private RiseNumberTextView tvAmountClickNum;
    /**
     * 转发
     */
    private LinearLayout llForward;
    private RiseNumberTextView tvAmountForwardNum;
    private TuiGuangAdapter statisticAdapter;
    private int ascId = 2;
    private int orderId = 1;
    private final int TIME = 1;
    private final int EXPOSE = 2;
    private final int CLICK = 3;
    private final int FORWARD = 4;
    private final int RATE = 5;

    private boolean isGetData;
    private int uid;
    private TextView save_money;
    private List<ShareArticlesRes.DataBean> statisticList;
    private RecyclerView recyclerView;
    private int currentPage = 1;
    private List<ShareArticlesRes.DataBean> fanDatas = new ArrayList<>();
    private ImageView Image_empty;
    private TextView title;
    private TextView tvClickRateNum;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_statistic_pro;
    }

    @Override
    protected void findView(View rootView) {
        recyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerView);
        save_money = (TextView) rootView.findViewById(R.id.save_money);
        llExposeNum = (LinearLayout) rootView.findViewById(R.id.llExposeNum);
        tvAmountExposeNum = (RiseNumberTextView) rootView.findViewById(R.id.tvAmountExposeNum);
        tvClickRateNum = (TextView) rootView.findViewById(R.id.tvClickRateNum);
        llClickNum = (LinearLayout) rootView.findViewById(R.id.llClickNum);
        tvAmountClickNum = (RiseNumberTextView) rootView.findViewById(R.id.tvAmountClickNum);
        llForward = (LinearLayout) rootView.findViewById(R.id.llForward);
        tvAmountForwardNum = (RiseNumberTextView) rootView.findViewById(R.id.tvAmountForwardNum);
        Image_empty = (ImageView) rootView.findViewById(R.id.Img_Empty);
        title = (TextView) rootView.findViewById(R.id.title);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);

        recyclerView.setLayoutManager(linearLayoutManager);
        Glide.with(getActivity())
                .load(R.mipmap.empty)
                .asGif()
                .into(Image_empty);

    }


    @Override
    protected void initData() {

        if (statisticAdapter == null) {
            statisticAdapter = new TuiGuangAdapter(getActivity());
        } else {
            statisticAdapter.notifyDataSetChanged();
        }
        uid = SharedPreferenceUtils.getMemberId(getActivity());
        int isCompany = 1;
        if (isCompany == 0) {//个人版
            title.setVisibility(View.VISIBLE);
        } else if (isCompany == 1) {//企业版
            title.setVisibility(View.GONE);
        }
        orderId = TIME;
        isGetData = true;
        statisticAdapter.openLoadAnimation();
//        recyclerView.addItemDecoration(new LinearItemDecoration(getResources().getColor(R.color.gray4)));
        recyclerView.setAdapter(statisticAdapter);
        getArticleList(true);
        getAllstatisticsData();
        registerListener();
    }

    @Override
    public void onResume() {
        super.onResume();
        int isCompany = 1;
        if (isCompany == 0) {//个人版
            title.setVisibility(View.VISIBLE);
        } else if (isCompany == 1) {//企业版
            title.setVisibility(View.GONE);
        }
        getAllstatisticsData();
    }

    private void getAllstatisticsData() {
        ArticleServiceImpl.getShareArticleStatisticalRestul(uid + "", new NetworkSubscriber<SumStatisticalRes>() {
            @Override
            public int onSuccess(SumStatisticalRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    SumStatisticalRes.DataBean statisticalRestul = data.getData();

                    NumberFormat ddf1 = NumberFormat.getNumberInstance();
                    ddf1.setMaximumFractionDigits(2);
                    ddf1.setMinimumFractionDigits(2);
                    save_money.setText(ddf1.format(statisticalRestul.getMoney()) + "");
                    double rate = statisticalRestul.getClick_rate();
                    tvClickRateNum.setText(ddf1.format(rate) + "%");
                    long sumExposeNum = statisticalRestul.getLook_num();
                    long sumClickNum = statisticalRestul.getClick_num();
                    long sumForwardNum = statisticalRestul.getTrans_num();
//                    sumExposeNum = 35436547;
//                    sumClickNum=242;
//                    sumForwardNum=346545;
                    final Object[] exposeNum = LongNumTransUtils.calculateNum(sumExposeNum);
                    tvAmountExposeNum.setText(exposeNum[0] + "" + exposeNum[1]);
                    final Object[] clickNum = LongNumTransUtils.calculateNum(sumClickNum);
                    tvAmountClickNum.setText(clickNum[0] + "" + clickNum[1]);
                    final Object[] forwardNum = LongNumTransUtils.calculateNum(sumForwardNum);
                    tvAmountForwardNum.setText(forwardNum[0] + "" + forwardNum[1]);

                    tvAmountExposeNum.withNumber(exposeNum[0]);
                    tvAmountClickNum.withNumber(clickNum[0]);
                    tvAmountForwardNum.withNumber(forwardNum[0]);

                    tvAmountExposeNum.setOnEndListener(new RiseNumberTextView.EndListener() {
                        @Override
                        public void onEndFinish() {
                            tvAmountExposeNum.setText(exposeNum[0] + "" + exposeNum[1]);
                        }
                    });

                    tvAmountClickNum.setOnEndListener(new RiseNumberTextView.EndListener() {
                        @Override
                        public void onEndFinish() {
                            tvAmountClickNum.setText(clickNum[0] + "" + clickNum[1]);
                        }
                    });

                    tvAmountForwardNum.setOnEndListener(new RiseNumberTextView.EndListener() {
                        @Override
                        public void onEndFinish() {
                            tvAmountForwardNum.setText(forwardNum[0] + "" + forwardNum[1]);
                        }
                    });

                    if (orderId == EXPOSE) {
                        tvAmountExposeNum.start();

                    } else if (orderId == CLICK) {
                        tvAmountClickNum.start();

                    } else if (orderId == FORWARD) {
                        tvAmountForwardNum.start();

                    } else {
                        tvAmountExposeNum.start();
                        tvAmountClickNum.start();
                        tvAmountForwardNum.start();
                    }
                } else {
                    LogUtils.startToash(getContext(), data.getMsg());
                }
                return 0;
            }
        });
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            JAnalyticsInterface.onPageStart(getContext(), "推广-个人数据");
        } else {
            JAnalyticsInterface.onPageEnd(getContext(), "推广-个人数据");
        }
    }

    public void registerListener() {
        llExposeNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (EXPOSE == orderId) {
                    orderId = TIME;
                    getArticleList(true);
                } else {
                    orderId = EXPOSE;
                    getArticleList(true);
                }
            }
        });
        llClickNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (CLICK == orderId) {
                    orderId = TIME;
                    getArticleList(true);
                } else {
                    orderId = CLICK;
                    getArticleList(true);
                }
            }
        });
        llForward.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (FORWARD == orderId) {
                    orderId = TIME;
                    getArticleList(true);
                } else {
                    orderId = FORWARD;
                    getArticleList(true);
                }
            }
        });
    }


/*
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(NotifyEvent event) {
        if (event.getCode() == EventCode.LOGIN_SUCCESS) {
            getArticleList(true);
        }

    }
*/


    @Override
    protected void notifyResume() {
        super.notifyResume();
        if (!isGetData) {
            getArticleList(true);
        }

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!isGetData) {
                getArticleList(true);
            }
        }
    }

    public void getArticleList(final boolean refresh1) {
        if (refresh1) {
            currentPage = 1;
        } else {
            currentPage++;
        }
        ArticleServiceImpl.getShareArticles(SharedPreferenceUtils.getMemberId(getActivity()) + "", currentPage, 20, new NetworkSubscriber<ShareArticlesRes>() {
            @Override
            public int onSuccess(ShareArticlesRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    if (currentPage == 1) {
                        fanDatas.clear();
                    }
                    fanDatas.addAll(data.getData());
                    if (fanDatas.size() > 0) {
                        Image_empty.setVisibility(View.GONE);
                    } else {
                        Image_empty.setVisibility(View.VISIBLE);
                    }
                    statisticAdapter.setNewData(fanDatas);
                    statisticAdapter.notifyDataSetChanged();
                    if (refresh1) {
                        new Handler().post(new Runnable() {
                            @Override
                            public void run() {
                                recyclerView.scrollToPosition(0);
                            }
                        });
                    }
                } else {
                    LogUtils.startToash(getContext(), data.getMsg());

                }
                return 0;
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
            }
        });
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
